
package commonlib.domain;

import java.util.*;

/**
 *
 * @ Todos
 */

public class NodeData
{
    // VARIABLES: --------------------------------------------------------------

    // aquest array es recorre a partir de l'index j (recorrer wordS)
    // { a->a , a -> ak , a -> aks , a -> aksu , a -> aksua , a -> aksual }
    private BetaCombination[] probabilities;

    // CONSTRUCTOR: ------------------------------------------------------------

    public NodeData(int size)
    {
        this.probabilities = new BetaCombination[size];
        for (int i = 0; i < probabilities.length; i++)
        {
            this.probabilities[i] = new BetaCombination();
        }
    }

    // GETTERS: ----------------------------------------------------------------

    public ArrayList<ChanceCombination> getProbabilities(int j)
    {
        return probabilities[j].getInfo();
    }

    // ADDERS: -----------------------------------------------------------------

    public void addProbability(double probability, int numRulesApplied, int j)
    {
        probabilities[j].addProbability(probability, numRulesApplied, j);
    }

    public void setProbability(double probability, int numRulesApplied, int j)
    {
        probabilities[j].setProbability(probability, j);
        probabilities[j].setNumRulesApplied(numRulesApplied, j);
    }

    // OTHER: ------------------------------------------------------------------

    public boolean areAnyProbabilities()
    {
        for (int i = 0; i < probabilities.length; i++)
        {
            if (!probabilities[i].getInfo().isEmpty()
                && !probabilities[i].isIrrelevant())
            {
                return true;
            }
        }
        return false;
    }

    public boolean isEmpty(int j)
    {
        boolean foo = probabilities[j].isEmpty();
        return foo;
    }

    // +-----------------------------------------------------------------------+
    // | INNER CLASS : BetaCombination                                         |
    // +-----------------------------------------------------------------------+

    private class BetaCombination
    {
        // VARIABLES: ----------------------------------------------------------

        private ArrayList<ChanceCombination> combinations;

        // CONSTRUCTOR: --------------------------------------------------------

        public BetaCombination()
        {
            combinations = new ArrayList<ChanceCombination>();
        }

        public BetaCombination(double probability, int numRulesApplied)
        {
            combinations = new ArrayList<ChanceCombination>();
            ChanceCombination foo;
            foo = new ChanceCombination(probability, numRulesApplied);
            combinations.add(foo);
        }

        // GETTERS: ------------------------------------------------------------

        public double getProbability(int i)
        {
            return this.combinations.get(i).getProbability();
        }

        public int getNumRulesApplied(int i)
        {
            return this.combinations.get(i).getNumRulesApplied();
        }

        // SETTERS: ------------------------------------------------------------

        public void setProbability(double probability, int i)
        {
            this.combinations.get(i).setProbability(probability);
        }

        public void setNumRulesApplied(int numRulesApplied, int i)
        {
            this.combinations.get(i).setNumRulesApplied(numRulesApplied);
        }

        // OTHER METHODS: ------------------------------------------------------

        public void addProbability(double prob, int numRulesApplied, int i)
        {
            combinations.add( new ChanceCombination(prob, numRulesApplied) );
        }

        public ArrayList<ChanceCombination> getInfo()
        {
            ArrayList<ChanceCombination> foo = new ArrayList<ChanceCombination>();
            if (!this.combinations.isEmpty()) return this.combinations;
            return foo;
        }

        public boolean isIrrelevant()
        {
            double minRelevant = 0.3;
            for (ChanceCombination foo : this.combinations)
            {
                if (foo.getProbability() > minRelevant) return false;
            }
            return true;
        }

        public boolean isEmpty()
        {
            boolean foo = this.combinations.isEmpty();
            return foo;
        }
    }
}